<?php

if (!defined('PUBLIC_HTML'))
{
	exit;
}

/*if (!$user->is_admin)
{
	redirect('cms.php?p=pages');
	exit;
}*/

$id = (int) $_GET['id'];

if ($id == 0 || !is_numeric($id))
{
	redirect('cms.php?p=pages');
	exit;
}

/*if ($database->sqlCount(TABLE_PAGES, 'page_id', $id) == '0')
{
	redirect('cms.php?p=pages');
	exit;
}*/

// Define language
$language = (isset($_GET['lang'])) ? $_GET['lang'] : 'en';

/***********************
* ADDING THE PAGE
************************/
$submit	= (isset($_POST['submit'])) ? true : false;

if ($submit)
{
	$post = array();
	$post['page_mod_id'] = 0;
	$post['page_parent_id'] = 0;
	$post['page_title']	= $_POST['page_title'];
	$post['page_full'] = 1;
	$post['page_type'] = 4;
	
	$database->sqlUpdate(TABLE_PAGES, $post, 'page_id', $id);
	
	$get_added_page = 	"
						SELECT		page_id
						FROM		" . TABLE_PAGES . "
						ORDER BY	page_id
						DESC
						LIMIT		0,1
						";
	$database->query($get_added_page);
	$row_added_page = $database->single();
	
	unset($post);
	$post = array();
	$post['pct_page_id'] 	= $row_added_page['page_id'];
	$post['pct_lang']		= 'en';
	$post['pct_var']		= $string->clean($_POST['page_title']);
	$post['pct_type']		= 'default';
	$post['pct_title']		= (string) $_POST['page_title'];
	$post['pct_content']	= (string) $_POST['content'];
	$post['pct_module']		= '';
	$post['pct_module_part']= '';
	$post['pct_row']		= '1';
	$post['pct_rowspan']	= '1';
	$post['pct_col']		= '1';
	$post['pct_colspan']	= '1';
	$post['pct_position']	= '1';
	$database->sqlUpdate(TABLE_PAGES_CONTENT, $post, 'pct_page_id', $id);
	
	/*if ($_POST['add_menu'] == 1)
	{
		$post = array();
		$post['page_id']	= $row_added_page['page_id'];
		$post['link']		= $added_page_var;
		$post['target']		= (isset($_POST['menu_target']))		? ((!empty($_POST['menu_target']))	? (string) $string->secure($_POST['menu_target'],	'sql_single_field') : '_self') 		: '_self';
		$post['title']		= (isset($_POST['menu_title']))		? ((!empty($_POST['menu_title']))	? (string) $string->secure($_POST['menu_title'],		'sql_single_field') : 'undefined') 	: 'undefined';
		$post['lang']		= 'en';
		$database->sqlUpdate(TABLE_MENU, $post, 'men_');
	}*/
	
	redirect('cms.php?p=pages');
	exit;
}

// Get main page information
$get_page =	"
			SELECT		*
			FROM		" . TABLE_PAGES . "
			WHERE		page_id = :page_id
			LIMIT		0,1
			";
$database->query($get_page);
$database->bind(':page_id', $id);
$row_page = $database->single();
$tpl->set_vars('TITLE', (string) $row_page['page_title']);

// Get page contents
$get_content =	"
				SELECT		*
				FROM		" . TABLE_PAGES_CONTENT . "
				WHERE		pct_page_id = :pct_page_id
				AND			pct_lang = :pct_lang
				ORDER BY	pct_position
				ASC
				";
$database->query($get_content);
$database->bind(':pct_page_id', $id);
$database->bind(':pct_lang', 'en');
foreach ($database->resultSet() as $row_content)
{
	$tpl->zone('content', array(
		'id'		=> $row_content['pct_id'],
		'var'		=> $row_content['pct_var'],
		'type'		=> $row_content['pct_type'],
		'title'		=> $row_content['pct_title'],
		'content'	=> $row_content['pct_content'],
		'position'	=> $row_content['pct_position']
	));
}

// Get Pages for the Subpage dropdown
$database->query("SELECT * FROM " . TABLE_PAGES . " WHERE page_parent_id = :page_parent_id");
$database->bind(':page_parent_id', 0);
foreach ($database->resultSet() as $row_pages)
{
	$tpl->zone('pages', array(
		'ID'		=> (int)	$row_pages['page_id'],
		'TITLE'		=> (string)	$row_pages['page_title'],
		'SELECTED'	=> ($row_pages['page_id'] == $id) ? ' selected="selected"' : ''
	));
}


?>